package model

import entity.MyContract
import entity.PbcEbookEntity
import presenter.MainPresenter
import tornadofx.*
import util.FileZipUtils
import util.MyDbHelper
import java.io.File

class FileControlModel(val mainPresenter: MainPresenter) {

    fun removeEbooks(removeBooks: PbcEbookEntity) {

        val removedFileList = MyDbHelper.instance.selectContentListByPath(removeBooks.path)
        println(removedFileList)
        removedFileList.forEach {
            var deleFilePath: String = it
            if (it.endsWith(".html") && it.lastIndexOf(File.separator) > 0) {
                deleFilePath = it.substring(0, it.lastIndexOf(File.separator))
            }

            println(deleFilePath)
            val deleteFile = File(deleFilePath)
            if (deleteFile.exists()) {
                deleteFile.deleteRecursively()
            }
        }

    }

    fun exportDatas(desPath: String) {
        runAsync {
            FileZipUtils.zip(File(MyContract.DATAS_BASE_PATH), "$desPath/datas.zip")
        } ui {
//            mainPresenter.imoprtOrExportDatasSuccess("导出成功！")
        }
    }

    fun importDatas(srcFile: File) {
        runAsync {
            FileZipUtils.unZip(srcFile, MyContract.DATAS_BASE_PATH)
        } ui {
//            mainPresenter.imoprtOrExportDatasSuccess("导入成功！")
        }
    }
}